/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tcc.sglj.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.Transaction;

import tcc.sglj.model.*;
import tcc.sglj.dao.DaoUser;
import tcc.sglj.app.MaintainUserApp;
import tcc.sglj.app.Authenticate;
import tcc.sglj.util.Encrypt;
import tcc.sglj.util.HibernateUtil;

/**
 *
 * @author Jhonatan L S Santos
 */
@Namespace(value = "/user")
@ParentPackage(value = "json-default")
public class MaintainUserAction {
    
    private boolean success;
    private String message;
    private User user;
    private boolean log;
    
    @Action(value = "sign", results =
    @Result(name = "success", type = "json", params = {"excludeProperties", "log"}))
    public String sign() {
        try {
            
            if (ServletActionContext.getRequest().getParameter("user.id").equals("")) {
                user.setPassword(new Encrypt(user.getPassword()).encrypt());
                user.setSign(new Date());
            } else {
                User a = new DaoUser().getUserById(user.getId());
                user.setPassword(a.getPassword());
                user.setSign(a.getSign());
            }
            
            User s = (User) ServletActionContext.getContext().getSession().get("user");
            if (!s.isPermission()) {
                user.setPermission(false);
            }
            
            MaintainUserApp maintain = new MaintainUserApp();
            
            if (maintain.sign(user)) {
                
                if (s.getId() == user.getId()) {
                    ServletActionContext.getContext().getSession().put("user", user);
                }
                setSuccess(true);
                setMessage("Cadastro realizado com sucesso!");
            } else {
                setSuccess(false);
                setMessage(maintain.getErrorMessage());
            }
            
        } catch (Exception e) {
            setSuccess(false);
            setMessage(e.getCause().toString());
        }
        
        return "success";
    }
    
    @Action(value = "delete", results = {
        @Result(name = "success", type = "json", params = {"excludeProperties", "user"})
    })
    public String delete() {
        try {
            
            MaintainUserApp maintain = new MaintainUserApp();
            boolean re = maintain.delete(Integer.parseInt(ServletActionContext.getRequest().getParameter("id")));
            
            if (re) {
                setSuccess(true);
                setMessage("Exclusão realizado com sucesso!");
                User s = (User) ServletActionContext.getContext().getSession().get("user");
                User a = new DaoUser().getUserById(Integer.parseInt(ServletActionContext.getRequest().getParameter("id")));
                if (a.getId() == s.getId()) {
                    ServletActionContext.getContext().getSession().put("user", null);
                    setLog(true);
                }
            } else {
                setSuccess(false);
                setMessage(maintain.getErrorMessage());
            }
            
        } catch (Exception e) {
            setSuccess(false);
            setMessage(e.getMessage());
        }
        return "success";
    }
    
    @Action(value = "get", results = {
        @Result(name = "success", type = "json")
    })
    public String get() {
        User u = (User) ServletActionContext.getContext().getSession().get("user");
        
        if (u != null) {
            setUser(u);
            setSuccess(true);
        } else {
            setSuccess(false);
        }
        return "success";
    }
    
    @Action(value = "email", results = {
        @Result(name = "success", type = "httpheader", params = {"status", "200"})
    })
    public String email() throws Exception {
        DaoUser dao = new DaoUser();
        User u = (User) dao.validEmail(ServletActionContext.getRequest().getParameter("email"));
        boolean email = false;
        String id = ServletActionContext.getRequest().getParameter("id");
        
        if (u != null) {
            if (!id.equals("")) {
                if (u.getId() == Integer.parseInt(id)) {
                    email = true;
                }
            }
        } else {
            email = true;
        }
        
        ServletActionContext.getResponse().getWriter().write(new Boolean(email).toString());
        return "success";
    }
    
    @Action(value = "cpf", results = {
        @Result(name = "success", type = "httpheader", params = {"status", "200"})
    })
    public String cpf() throws Exception {
        DaoUser dao = new DaoUser();
        User u = (User) dao.validCPF(ServletActionContext.getRequest().getParameter("cpf"));
        boolean cpf = false;
        String id = ServletActionContext.getRequest().getParameter("id");
        
        if (u != null) {
            if (!id.equals("")) {
                if (u.getId() == Integer.parseInt(id)) {
                    cpf = true;
                }
            }
        } else {
            cpf = true;
        }
        
        ServletActionContext.getResponse().getWriter().write(new Boolean(cpf).toString());
        return "success";
    }

    /**
     * @return the message
     */
    public String getMessage() {
        return message;
    }

    /**
     * @param message the message to set
     */
    public void setMessage(String message) {
        this.message = message;
    }

    /**
     * @return the success
     */
    public boolean getSuccess() {
        return success;
    }

    /**
     * @param success the success to set
     */
    public void setSuccess(boolean success) {
        this.success = success;
    }

    /**
     * @return the user
     */
    public User getUser() {
        return user;
    }

    /**
     * @param user the user to set
     */
    public void setUser(User user) {
        this.user = user;
    }

    /**
     * @return the log
     */
    public boolean isLog() {
        return log;
    }

    /**
     * @param log the log to set
     */
    public void setLog(boolean log) {
        this.log = log;
    }
}
